package com.sise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sise.bean.CodeMsg;
import com.sise.entity.OrderMaster;
import com.sise.entity.OrderRefund;
import com.sise.mapper.OrderMasterMapper;
import com.sise.mapper.OrderRefundMapper;
import com.sise.service.IOrderRefundService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zj
 */
@Service
public class OrderRefundServiceImpl extends ServiceImpl<OrderRefundMapper, OrderRefund> implements IOrderRefundService {

    @Resource
    private OrderRefundMapper orderRefundMapper;

    @Resource
    private OrderMasterMapper orderMasterMapper;

    @Override
    public CodeMsg applyOrderRefund(OrderRefund orderRefund) {
        QueryWrapper<OrderMaster> orderMasterQueryWrapper = new QueryWrapper<>();
        orderMasterQueryWrapper.lambda().eq(OrderMaster::getOrderId, orderRefund.getOrderId());
        OrderMaster orderMaster = orderMasterMapper.selectOne(orderMasterQueryWrapper);
        if (orderMaster != null) {
            String msg;
            if (orderMaster.getOrderStatus() == 0) {
                orderRefund.setRefundStatus(2);
                msg = "订单为待付款状态，稍后订单自动取消";
            } else if (orderMaster.getOrderStatus() == 1) {
                orderRefund.setRefundStatus(1).setRefundMoney(orderMaster.getTotalOrderAmount());
                orderRefundMapper.insert(orderRefund);
                return new CodeMsg().setCode(200).setMsg("退款成功");
            } else if (orderMaster.getOrderStatus() == 2) {
                orderRefund.setRefundStatus(2);
                msg = "订单状态出错";
            } else if (orderMaster.getOrderStatus() == 3) {
                orderRefund.setRefundStatus(2);
                msg = "订单已完成，无法退款";
            } else if (orderMaster.getOrderStatus() == 4) {
                orderRefund.setRefundStatus(2);
                msg = "订单已取消";
            } else if (orderMaster.getOrderStatus() == 5) {
                orderRefund.setRefundStatus(2);
                msg = "订单已退款";
            } else {
                orderRefund.setRefundStatus(2);
                msg = "订单出错";
            }
            orderRefund.setRefundMoney(orderMaster.getTotalOrderAmount());
            orderRefundMapper.insert(orderRefund);
            return new CodeMsg().setCode(400).setMsg(msg);
        } else {
            return new CodeMsg().setCode(400).setMsg("订单号不存在");
        }
    }

    @Override
    public CodeMsg getOrderRefund(String orderId) {
        QueryWrapper<OrderRefund> orderRefundQueryWrapper = new QueryWrapper<>();
        orderRefundQueryWrapper.lambda().eq(OrderRefund::getOrderId, orderId);
        OrderRefund orderRefund = orderRefundMapper.selectOne(orderRefundQueryWrapper);
        if (orderRefund != null) {
            return new CodeMsg().setCode(200).setData(orderRefund);
        } else {
            return new CodeMsg().setCode(400).setMsg("没有订单退款信息");
        }
    }
}
